﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLChuongTrinhDaoTao
    {
        
        List<ChuongTrinhDaoTao> list;
        QuanLySVDataContext db;


        // constructor
        public BLLChuongTrinhDaoTao()
        {
            this.list = new QuanLySVDataContext().ChuongTrinhDaoTaos.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 CTDT co ton tai hay khong
        public bool checkExist(ChuongTrinhDaoTao temp)
        {
            foreach (ChuongTrinhDaoTao dt in list)
            {
                if (dt.CTDT_ID.Equals(temp.CTDT_ID))
                    return true;
            }
            return false;
        }

        // update thong tin CTDT
        public bool updateCTDT(ChuongTrinhDaoTao update)
        {
            try
            {

                var temp = (from c in db.ChuongTrinhDaoTaos
                            where c.CTDT_ID == update.CTDT_ID
                            select c).First();
                ChuongTrinhDaoTao found = (ChuongTrinhDaoTao)temp;
                found.CTDT_ID = update.CTDT_ID;
                found.LopID = update.LopID;
                found.NganhID = update.NganhID;
               
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them CTDT moi vao database
        public bool insertCTDT(ChuongTrinhDaoTao temp)
        {
            try
            {
                db.ChuongTrinhDaoTaos.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa CTDT
        public bool deleteCTDT(ChuongTrinhDaoTao temp)
        {
            try
            {
                var dt = (from c in db.ChuongTrinhDaoTaos
                          where c.CTDT_ID == temp.CTDT_ID
                          select c).First();
                ChuongTrinhDaoTao found = (ChuongTrinhDaoTao)dt;

                db.ChuongTrinhDaoTaos.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac CTDT
        public List<ChuongTrinhDaoTao> returnListCTDT()
        {
            this.list = new QuanLySVDataContext().ChuongTrinhDaoTaos.ToList();
            return list;
        }

        // tra ve 1 CTDT theo so thu tu trong list
        public ChuongTrinhDaoTao retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().ChuongTrinhDaoTaos.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else return null;
        }

        // tra ve 1 CTDT theo ma CTDT
        public ChuongTrinhDaoTao retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().ChuongTrinhDaoTaos.ToList();
            foreach (ChuongTrinhDaoTao temp in list)
                if (temp.CTDT_ID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 CTDT co hop le hay khong
        public bool kiemTra(ChuongTrinhDaoTao temp)
        {
            if (temp.CTDT_ID == "") return false;
            return true;
        }
    }
}
